# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        def length(head):
            leng = 0
            while head:
                head = head.next
                leng+=1
            return leng
        lena = length(headA)
        lenb = length(headB)
        dif = abs(lena-lenb)
        if lena<lenb: headA, headB = headB, headA
        for _ in range(dif):
            headA = headA.next
        while headA and headB:
            if headA==headB:return headA
            headA, headB = headA.next, headB.next
        return None